package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.foxcard.UserFoxCardMonster;
import com.yanqu.road.entity.activity.foxcard.UserFoxCardShop;
import com.yanqu.road.entity.activity.foxcard.UserFoxCardGameData;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class UserFoxCardGameDaoImpl extends TempDataObjectDao<UserFoxCardGameData> {
    public static String getUserFoxCardGameDataSql = "select * from `t_u_activity_fox_game` where `activityId`=? and `userId`=?";
    public UserFoxCardGameData getUserFoxCardGameData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserFoxCardGameDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserFoxCardGameDataSql = "update `t_u_activity_fox_game` set stageId=?,round=?,inBattle=?,effectCardNumLimit=?,consumeCardNumLimit=?,handCardNumLimit=?,effectCards=?,consumeCards=?,handCards=?,remainHandCards=?,dropHandCards=?,showCardTimes=?,dropCardTimes=?,showCardTimesMax=?,dropCardTimesMax=?,gold=?,damageAmount=?,waitChooseCards=?,remainChooseTimes=?,coupons=?,bossLimitEffect=?,skipLabelHaves=?,maxCardId=?,isShopping=?,comboInfoMap=?,consumeCardTimesMap=?,historyBuffTimesMap=?,showPointAmountMap=?,dropPointAmountMap=?,skipTimesAmount=?,showTimesAmount=?,dropTimesAmount=?,lastUsedConsumeCardId=?,roundLastShowCombo=?,roundLimitEffectCard=?,roundShowCombos=?,maxOnceDamage=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(UserFoxCardGameData userFoxCardGameData) {
        userFoxCardGameData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getStageId()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getRound()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userFoxCardGameData.getInBattle()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getEffectCardNumLimit()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getConsumeCardNumLimit()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getHandCardNumLimit()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getEffectCards())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getConsumeCards())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getHandCards())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getRemainHandCards())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getDropHandCards())));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getShowCardTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getDropCardTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getShowCardTimesMax()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getDropCardTimesMax()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getGold()));
        para.put(i++, new DbParameter(Types.VARCHAR, userFoxCardGameData.getDamageAmount().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getWaitChooseCards())));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getRemainChooseTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getCoupons())));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getBossLimitEffect()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getSkipLabelHaves())));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getMaxCardId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userFoxCardGameData.getIsShopping()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getComboInfoMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getConsumeCardTimesMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getHistoryBuffTimesMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getShowPointAmountMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getDropPointAmountMap())));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getSkipTimesAmount()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getShowTimesAmount()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getDropTimesAmount()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getLastUsedConsumeCardId()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getRoundLastShowCombo()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getRoundLimitEffectCard())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getRoundShowCombos())));
        para.put(i++, new DbParameter(Types.VARCHAR, userFoxCardGameData.getMaxOnceDamage().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userFoxCardGameData.getUserId()));
        if (getDb().executeNoneQuery(updateUserFoxCardGameDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserFoxCardGameDataSql = "insert into `t_u_activity_fox_game`(activityId,userId,stageId,round,inBattle,effectCardNumLimit,consumeCardNumLimit,handCardNumLimit,effectCards,consumeCards,handCards,remainHandCards,dropHandCards,showCardTimes,dropCardTimes,showCardTimesMax,dropCardTimesMax,gold,damageAmount,waitChooseCards,remainChooseTimes,coupons,bossLimitEffect,skipLabelHaves,maxCardId,isShopping,comboInfoMap,consumeCardTimesMap,historyBuffTimesMap,showPointAmountMap,dropPointAmountMap,skipTimesAmount,showTimesAmount,dropTimesAmount,lastUsedConsumeCardId,roundLastShowCombo,roundLimitEffectCard,roundShowCombos,maxOnceDamage) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserFoxCardGameData userFoxCardGameData) {
        userFoxCardGameData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userFoxCardGameData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getStageId()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getRound()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userFoxCardGameData.getInBattle()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getEffectCardNumLimit()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getConsumeCardNumLimit()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getHandCardNumLimit()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getEffectCards())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getConsumeCards())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getHandCards())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getRemainHandCards())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getDropHandCards())));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getShowCardTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getDropCardTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getShowCardTimesMax()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getDropCardTimesMax()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getGold()));
        para.put(i++, new DbParameter(Types.VARCHAR, userFoxCardGameData.getDamageAmount().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getWaitChooseCards())));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getRemainChooseTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getCoupons())));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getBossLimitEffect()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getSkipLabelHaves())));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getMaxCardId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userFoxCardGameData.getIsShopping()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getComboInfoMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getConsumeCardTimesMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getHistoryBuffTimesMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getShowPointAmountMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getDropPointAmountMap())));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getSkipTimesAmount()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getShowTimesAmount()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getDropTimesAmount()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getLastUsedConsumeCardId()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardGameData.getRoundLastShowCombo()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getRoundLimitEffectCard())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardGameData.getRoundShowCombos())));
        para.put(i++, new DbParameter(Types.VARCHAR, userFoxCardGameData.getMaxOnceDamage().toString()));
        if (getDb().executeNoneQuery(insertUserFoxCardGameDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserFoxCardGameData parseData(ResultSet rs) throws SQLException {
        UserFoxCardGameData userFoxCardGameData = new UserFoxCardGameData();
        userFoxCardGameData.setActivityId(rs.getInt("activityId"));
        userFoxCardGameData.setUserId(rs.getLong("userId"));
        userFoxCardGameData.setStageId(rs.getInt("stageId"));
        userFoxCardGameData.setRound(rs.getInt("round"));
        userFoxCardGameData.setInBattle(rs.getBoolean("inBattle"));
        userFoxCardGameData.setEffectCardNumLimit(rs.getInt("effectCardNumLimit"));
        userFoxCardGameData.setConsumeCardNumLimit(rs.getInt("consumeCardNumLimit"));
        userFoxCardGameData.setHandCardNumLimit(rs.getInt("handCardNumLimit"));
        userFoxCardGameData.setEffectCards(JSON.parseObject(rs.getString("effectCards"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.foxcard.FoxEffectCard>>() {}));
        userFoxCardGameData.setConsumeCards(JSON.parseObject(rs.getString("consumeCards"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.foxcard.FoxConsumeCard>>() {}));
        userFoxCardGameData.setHandCards(JSON.parseObject(rs.getString("handCards"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.foxcard.FoxHandCard>>() {}));
        userFoxCardGameData.setRemainHandCards(JSON.parseObject(rs.getString("remainHandCards"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.foxcard.FoxHandCard>>() {}));
        userFoxCardGameData.setDropHandCards(JSON.parseObject(rs.getString("dropHandCards"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.foxcard.FoxHandCard>>() {}));
        userFoxCardGameData.setShowCardTimes(rs.getInt("showCardTimes"));
        userFoxCardGameData.setDropCardTimes(rs.getInt("dropCardTimes"));
        userFoxCardGameData.setShowCardTimesMax(rs.getInt("showCardTimesMax"));
        userFoxCardGameData.setDropCardTimesMax(rs.getInt("dropCardTimesMax"));
        userFoxCardGameData.setGold(rs.getInt("gold"));
        userFoxCardGameData.setDamageAmount(new BigInteger(rs.getString("damageAmount")));
        userFoxCardGameData.setWaitChooseCards(JSON.parseObject(rs.getString("waitChooseCards"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.foxcard.FoxSaleCard>>() {}));
        userFoxCardGameData.setRemainChooseTimes(rs.getInt("remainChooseTimes"));
        userFoxCardGameData.setCoupons(JSON.parseObject(rs.getString("coupons"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        userFoxCardGameData.setBossLimitEffect(rs.getInt("bossLimitEffect"));
        userFoxCardGameData.setSkipLabelHaves(JSON.parseObject(rs.getString("skipLabelHaves"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        userFoxCardGameData.setMaxCardId(rs.getInt("maxCardId"));
        userFoxCardGameData.setIsShopping(rs.getBoolean("isShopping"));
        userFoxCardGameData.setComboInfoMap(JSON.parseObject(rs.getString("comboInfoMap"), new TypeReference<java.util.Map<java.lang.Integer, java.util.List<java.lang.Integer>>>() {}));
        userFoxCardGameData.setConsumeCardTimesMap(JSON.parseObject(rs.getString("consumeCardTimesMap"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        userFoxCardGameData.setHistoryBuffTimesMap(JSON.parseObject(rs.getString("historyBuffTimesMap"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        userFoxCardGameData.setShowPointAmountMap(JSON.parseObject(rs.getString("showPointAmountMap"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        userFoxCardGameData.setDropPointAmountMap(JSON.parseObject(rs.getString("dropPointAmountMap"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        userFoxCardGameData.setSkipTimesAmount(rs.getInt("skipTimesAmount"));
        userFoxCardGameData.setShowTimesAmount(rs.getInt("showTimesAmount"));
        userFoxCardGameData.setDropTimesAmount(rs.getInt("dropTimesAmount"));
        userFoxCardGameData.setLastUsedConsumeCardId(rs.getInt("lastUsedConsumeCardId"));
        userFoxCardGameData.setRoundLastShowCombo(rs.getInt("roundLastShowCombo"));
        userFoxCardGameData.setRoundLimitEffectCard(JSON.parseObject(rs.getString("roundLimitEffectCard"), Set.class));
        userFoxCardGameData.setRoundShowCombos(JSON.parseObject(rs.getString("roundShowCombos"), Set.class));
        userFoxCardGameData.setMaxOnceDamage(new BigInteger(rs.getString("maxOnceDamage")));

        return userFoxCardGameData;
    }
}
